Page({
  data: {
    title: '',
    templateId: '',
    templateName: '',
    signers: [{ name: '', phone: '' }],
    contractFile: null
  },

  // 选择合同模板
  selectTemplate() {
    wx.navigateTo({
      url: '/pages/template/list?select=true'
    })
  },

  // 添加签署方
  addSigner() {
    const signers = this.data.signers
    signers.push({ name: '', phone: '' })
    this.setData({ signers })
  },

  // 删除签署方
  deleteSigner(e) {
    const index = e.currentTarget.dataset.index
    const signers = this.data.signers
    signers.splice(index, 1)
    this.setData({ signers })
  },

  // 更新签署方信息
  updateSignerName(e) {
    const { index } = e.currentTarget.dataset
    const { value } = e.detail
    const signers = this.data.signers
    signers[index].name = value
    this.setData({ signers })
  },

  updateSignerPhone(e) {
    const { index } = e.currentTarget.dataset
    const { value } = e.detail
    const signers = this.data.signers
    signers[index].phone = value
    this.setData({ signers })
  },

  // 上传合同文件
  uploadContract() {
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      extension: ['pdf'],
      success: (res) => {
        const file = res.tempFiles[0]
        wx.showLoading({ title: '上传中' })
        
        wx.uploadFile({
          url: 'your-api-url/upload/contract',
          filePath: file.path,
          name: 'file',
          success: (uploadRes) => {
            const data = JSON.parse(uploadRes.data)
            this.setData({
              contractFile: {
                name: file.name,
                url: data.url
              }
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      }
    })
  },

  // 创建合同
  async createContract() {
    if (!this.validateForm()) {
      return
    }

    wx.showLoading({ title: '创建中' })
    try {
      const res = await wx.request({
        url: 'your-api-url/contract/create',
        method: 'POST',
        data: {
          title: this.data.title,
          templateId: this.data.templateId,
          signers: this.data.signers,
          fileUrl: this.data.contractFile.url
        }
      })

      if (res.data.success) {
        wx.showToast({
          title: '创建成功',
          icon: 'success'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      }
    } catch (error) {
      wx.showToast({
        title: '创建失败',
        icon: 'none'
      })
    }
  },

  // 表单验证
  validateForm() {
    if (!this.data.title) {
      wx.showToast({
        title: '请输入合同标题',
        icon: 'none'
      })
      return false
    }
    if (!this.data.templateId) {
      wx.showToast({
        title: '请选择合同模板',
        icon: 'none'
      })
      return false
    }
    
    const validSigners = this.data.signers.every(signer => 
      signer.name && signer.phone
    )
    if (!validSigners) {
      wx.showToast({
        title: '请完善签署方信息',
        icon: 'none'
      })
      return false
    }

    if (!this.data.contractFile) {
      wx.showToast({
        title: '请上传合同文件',
        icon: 'none'
      })
      return false
    }
    return true
  }
}) 